package model;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import util.HtmlUtil;
import util.StringUtil;

import bll.ITreeNode;
import dal.*;

public class AgendaItemModel implements IModel, ITreeNode {

	public static class FieldNames {
		public static String Id = "Id";
		public static String IdAgenda = "IdAgenda";
		public static String IdParent = "IdParent";
		public static String Name = "Name";
		public static String Description = "Description";
		public static String Remark = "Remark";
		public static String IsGroup = "IsGroup";
		public static String IdPrevItem = "IdPrevItem";
		public static String StartTime = "StartTime";
		public static String Duration = "Duration";
		public static String IdEmployee = "IdEmployee";
		public static String IdCompany = "IdCompany";
		public static String Sound = "Sound";
		public static String Light = "Light";
		public static String LeftScreen = "LeftScreen";
		public static String RightScreen = "RightScreen";
		public static String CenterScreen = "CenterScreen";
		public static String Code = "Code";
		public static String IdType = "IdType";
		public static String Status = "Status";
		public static String Notification = "Notification";
		public static String Created = "Created";
		public static String Creator = "Creator";
		public static String Updated = "Updated";
		public static String Updator = "Updator";
		public static String Deleted = "Deleted";
		public static String DeletionTime = "DeletionTime";

	}

	public class ErrorMessages {
		public String Id = "";
		public String IdAgenda = "";
		public String IdParent = "";
		public String Name = "";
		public String Description = "";
		public String Remark = "";
		public String IsGroup = "";
		public String IdPrevItem = "";
		public String StartTime = "";
		public String Duration = "";
		public String IdEmployee = "";
		public String IdCompany = "";
		public String Sound = "";
		public String Light = "";
		public String LeftScreen = "";
		public String RightScreen = "";
		public String CenterScreen = "";
		public String Code = "";
		public String IdType = "";
		public String Status = "";
		public String Notification = "";
		public String Created = "";
		public String Creator = "";
		public String Updated = "";
		public String Updator = "";
		public String Deleted = "";
		public String DeletionTime = "";

	}

	public ErrorMessages errMsgs = new ErrorMessages();

	public String[] errMsgList = null;

	// private variables
	private int _id = Integer.MIN_VALUE;
	private int _id_agenda = Integer.MIN_VALUE;
	private int _id_parent = Integer.MIN_VALUE;
	private String _name = null;
	private String _description = null;
	private String _remark = null;
	private boolean _is_group = false;
	private boolean _isis_groupSet = false;
	private int _id_prev_item = Integer.MIN_VALUE;
	private Date _start_time = null;
	private int _duration = Integer.MIN_VALUE;
	private int _id_employee = Integer.MIN_VALUE;
	private int _id_company = Integer.MIN_VALUE;
	private String _sound = null;
	private String _light = null;
	private String _left_screen = null;
	private String _right_screen = null;
	private String _center_screen = null;
	private String _code = null;
	private int _id_type = Integer.MIN_VALUE;
	private int _status = Integer.MIN_VALUE;
	private int _notification = Integer.MIN_VALUE;
	private Date _created = null;
	private int _creator = Integer.MIN_VALUE;
	private Date _updated = null;
	private int _updator = Integer.MIN_VALUE;
	private boolean _deleted = false;
	private boolean _isdeletedSet = false;
	private Date _deletion_time = null;

	public AgendaItemModel() {
	}

	// get/set
	public int getId() {
		return _id;
	}

	public void setId(int value) {
		_id = value;
	}

	public String getIdToString() {
		if (_id == Integer.MIN_VALUE || _id == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id);
		}
	}

	public int getIdAgenda() {
		return _id_agenda;
	}

	public void setIdAgenda(int value) {
		_id_agenda = value;
	}

	public String getIdAgendaToString() {
		if (_id_agenda == Integer.MIN_VALUE || _id_agenda == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_agenda);
		}
	}

	public int getIdParent() {
		return _id_parent;
	}

	public void setIdParent(int value) {
		_id_parent = value;
	}

	public String getIdParentToString() {
		if (_id_parent == Integer.MIN_VALUE || _id_parent == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_parent);
		}
	}

	public String getName() {
		return _name;
	}

	public void setName(String value) {
		_name = value;
	}

	public String getNameToString() {
		if (_name != null) {
			return HtmlUtil.genHtml(_name);
		} else {
			return "";
		}
	}

	public String getDescription() {
		return _description;
	}

	public void setDescription(String value) {
		_description = value;
	}

	public String getDescriptionToString() {
		if (_description != null) {
			return HtmlUtil.genHtml(_description);
		} else {
			return "";
		}
	}

	public String getRemark() {
		return _remark;
	}

	public void setRemark(String value) {
		_remark = value;
	}

	public String getRemarkToString() {
		if (_remark != null) {
			return HtmlUtil.genHtml(_remark);
		} else {
			return "";
		}
	}

	public boolean getIsGroup() {
		return _is_group;
	}

	public void setIsGroup(boolean value) {
		_is_group = value;
		_isis_groupSet = true;
	}

	public boolean getIsGroupSet() {
		return _isis_groupSet;
	}

	public String getIsGroupToString() {
		if (_isis_groupSet) {
			return HtmlUtil.genHtml(_is_group);
		} else {
			return "";
		}
	}

	public int getIdPrevItem() {
		return _id_prev_item;
	}

	public void setIdPrevItem(int value) {
		_id_prev_item = value;
	}

	public String getIdPrevItemToString() {
		if (_id_prev_item == Integer.MIN_VALUE || _id_prev_item == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_prev_item);
		}
	}

	public Date getStartTime() {
		return _start_time;
	}

	public void setStartTime(Date value) {
		_start_time = value;
	}

	public String getStartTimeToString() {
		if (_start_time != null) {
			return StringUtil.toDateTimeString(_start_time);
		} else {
			return "";
		}
	}

	public int getDuration() {
		return _duration;
	}

	public void setDuration(int value) {
		_duration = value;
	}

	public String getDurationToString() {
		if (_duration == Integer.MIN_VALUE || _duration == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_duration);
		}
	}

	public int getIdEmployee() {
		return _id_employee;
	}

	public void setIdEmployee(int value) {
		_id_employee = value;
	}

	public String getIdEmployeeToString() {
		if (_id_employee == Integer.MIN_VALUE || _id_employee == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_employee);
		}
	}

	public int getIdCompany() {
		return _id_company;
	}

	public void setIdCompany(int value) {
		_id_company = value;
	}

	public String getIdCompanyToString() {
		if (_id_company == Integer.MIN_VALUE || _id_company == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_company);
		}
	}

	public String getSound() {
		return _sound;
	}

	public void setSound(String value) {
		_sound = value;
	}

	public String getSoundToString() {
		if (_sound != null) {
			return HtmlUtil.genHtml(_sound);
		} else {
			return "";
		}
	}

	public String getLight() {
		return _light;
	}

	public void setLight(String value) {
		_light = value;
	}

	public String getLightToString() {
		if (_light != null) {
			return HtmlUtil.genHtml(_light);
		} else {
			return "";
		}
	}

	public String getLeftScreen() {
		return _left_screen;
	}

	public void setLeftScreen(String value) {
		_left_screen = value;
	}

	public String getLeftScreenToString() {
		if (_left_screen != null) {
			return HtmlUtil.genHtml(_left_screen);
		} else {
			return "";
		}
	}

	public String getRightScreen() {
		return _right_screen;
	}

	public void setRightScreen(String value) {
		_right_screen = value;
	}

	public String getRightScreenToString() {
		if (_right_screen != null) {
			return HtmlUtil.genHtml(_right_screen);
		} else {
			return "";
		}
	}

	public String getCenterScreen() {
		return _center_screen;
	}

	public void setCenterScreen(String value) {
		_center_screen = value;
	}

	public String getCenterScreenToString() {
		if (_center_screen != null) {
			return HtmlUtil.genHtml(_center_screen);
		} else {
			return "";
		}
	}

	public String getCode() {
		return _code;
	}

	public void setCode(String value) {
		_code = value;
	}

	public String getCodeToString() {
		if (_code != null) {
			return HtmlUtil.genHtml(_code);
		} else {
			return "";
		}
	}

	public int getIdType() {
		return _id_type;
	}

	public void setIdType(int value) {
		_id_type = value;
	}

	public String getIdTypeToString() {
		if (_id_type == Integer.MIN_VALUE || _id_type == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_type);
		}
	}

	public int getStatus() {
		return _status;
	}

	public void setStatus(int value) {
		_status = value;
	}

	public String getStatusToString() {
		if (_status == Integer.MIN_VALUE || _status == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_status);
		}
	}

	public int getNotification() {
		return _notification;
	}

	public void setNotification(int value) {
		_notification = value;
	}

	public String getNotificationToString() {
		if (_notification == Integer.MIN_VALUE || _notification == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_notification);
		}
	}

	public Date getCreated() {
		return _created;
	}

	public void setCreated(Date value) {
		_created = value;
	}

	public String getCreatedToString() {
		if (_created != null) {
			return StringUtil.toDateTimeString(_created);
		} else {
			return "";
		}
	}

	public int getCreator() {
		return _creator;
	}

	public void setCreator(int value) {
		_creator = value;
	}

	public String getCreatorToString() {
		if (_creator == Integer.MIN_VALUE || _creator == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_creator);
		}
	}

	public Date getUpdated() {
		return _updated;
	}

	public void setUpdated(Date value) {
		_updated = value;
	}

	public String getUpdatedToString() {
		if (_updated != null) {
			return StringUtil.toDateTimeString(_updated);
		} else {
			return "";
		}
	}

	public int getUpdator() {
		return _updator;
	}

	public void setUpdator(int value) {
		_updator = value;
	}

	public String getUpdatorToString() {
		if (_updator == Integer.MIN_VALUE || _updator == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_updator);
		}
	}

	public boolean getDeleted() {
		return _deleted;
	}

	public void setDeleted(boolean value) {
		_deleted = value;
		_isdeletedSet = true;
	}

	public boolean getDeletedSet() {
		return _isdeletedSet;
	}

	public String getDeletedToString() {
		if (_isdeletedSet) {
			return HtmlUtil.genHtml(_deleted);
		} else {
			return "";
		}
	}

	public Date getDeletionTime() {
		return _deletion_time;
	}

	public void setDeletionTime(Date value) {
		_deletion_time = value;
	}

	public String getDeletionTimeToString() {
		if (_deletion_time != null) {
			return StringUtil.toDateTimeString(_deletion_time);
		} else {
			return "";
		}
	}

	@Override
	public int[] getPrimaryKey() {
		int[] ids = new int[1];
		ids[0] = getId();
		return ids;
	}

	@Override
	public IModel readFromResultSet(ResultSet rs) throws SQLException {
		AgendaItemModel model = null;

		model = new AgendaItemModel();

		// read fields
		{
			int value = rs.getInt("id");
			if (!rs.wasNull()) {
				model.setId(value);
			}
		}
		{
			int value = rs.getInt("id_agenda");
			if (!rs.wasNull()) {
				model.setIdAgenda(value);
			}
		}
		{
			int value = rs.getInt("id_parent");
			if (!rs.wasNull()) {
				model.setIdParent(value);
			}
		}
		{
			String value = rs.getString("name");
			if (!rs.wasNull()) {
				model.setName(value);
			}
		}
		{
			String value = rs.getString("description");
			if (!rs.wasNull()) {
				model.setDescription(value);
			}
		}
		{
			String value = rs.getString("remark");
			if (!rs.wasNull()) {
				model.setRemark(value);
			}
		}
		{
			boolean value = rs.getBoolean("is_group");
			if (!rs.wasNull()) {
				model.setIsGroup(value);
			}
		}
		{
			int value = rs.getInt("id_prev_item");
			if (!rs.wasNull()) {
				model.setIdPrevItem(value);
			}
		}
		{
			Date value = rs.getTimestamp("start_time");
			if (!rs.wasNull()) {
				model.setStartTime(value);
			}
		}
		{
			int value = rs.getInt("duration");
			if (!rs.wasNull()) {
				model.setDuration(value);
			}
		}
		{
			int value = rs.getInt("id_employee");
			if (!rs.wasNull()) {
				model.setIdEmployee(value);
			}
		}
		{
			int value = rs.getInt("id_company");
			if (!rs.wasNull()) {
				model.setIdCompany(value);
			}
		}
		{
			String value = rs.getString("sound");
			if (!rs.wasNull()) {
				model.setSound(value);
			}
		}
		{
			String value = rs.getString("light");
			if (!rs.wasNull()) {
				model.setLight(value);
			}
		}
		{
			String value = rs.getString("left_screen");
			if (!rs.wasNull()) {
				model.setLeftScreen(value);
			}
		}
		{
			String value = rs.getString("right_screen");
			if (!rs.wasNull()) {
				model.setRightScreen(value);
			}
		}
		{
			String value = rs.getString("center_screen");
			if (!rs.wasNull()) {
				model.setCenterScreen(value);
			}
		}
		{
			String value = rs.getString("code");
			if (!rs.wasNull()) {
				model.setCode(value);
			}
		}
		{
			int value = rs.getInt("id_type");
			if (!rs.wasNull()) {
				model.setIdType(value);
			}
		}
		{
			int value = rs.getInt("status");
			if (!rs.wasNull()) {
				model.setStatus(value);
			}
		}
		{
			int value = rs.getInt("notification");
			if (!rs.wasNull()) {
				model.setNotification(value);
			}
		}
		{
			Date value = rs.getTimestamp("created");
			if (!rs.wasNull()) {
				model.setCreated(value);
			}
		}
		{
			int value = rs.getInt("creator");
			if (!rs.wasNull()) {
				model.setCreator(value);
			}
		}
		{
			Date value = rs.getTimestamp("updated");
			if (!rs.wasNull()) {
				model.setUpdated(value);
			}
		}
		{
			int value = rs.getInt("updator");
			if (!rs.wasNull()) {
				model.setUpdator(value);
			}
		}
		{
			boolean value = rs.getBoolean("deleted");
			if (!rs.wasNull()) {
				model.setDeleted(value);
			}
		}
		{
			Date value = rs.getTimestamp("deletion_time");
			if (!rs.wasNull()) {
				model.setDeletionTime(value);
			}
		}

		return model;
	}

	@Override
	public PreparedStatement genSelectStatement(Connection dbConn, IModel query) throws SQLException {
		String sql = "SELECT * FROM sp_agenda_item_select(:id);";
		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		AgendaItemModel obj = (AgendaItemModel) query;

		// set parameters
		st.setObject("id", obj.getId());

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genSelectListStatement(Connection dbConn, IModelExt query) throws SQLException {
		String sql = "SELECT * FROM sp_agenda_item_selectList(:limit, :offset, :id,:id_agenda,:id_parent,:name,:description,:remark,:is_group,:id_prev_item,:start_timeFrom,:start_timeTo,:duration,:id_employee,:id_company,:sound,:light,:left_screen,:right_screen,:center_screen,:code,:id_type,:status,:notification,:createdFrom,:createdTo,:creator,:updatedFrom,:updatedTo,:updator,:deleted,:deletion_timeFrom,:deletion_timeTo)";

		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		AgendaItemModelExt obj = (AgendaItemModelExt) query;

		if (obj.getLimit() != Integer.MIN_VALUE) {
			st.setObject("limit", obj.getLimit());
		} else {
			st.setObject("limit", Integer.MAX_VALUE);
		}
		if (obj.getOffset() != Integer.MIN_VALUE) {
			st.setObject("offset", obj.getOffset());
		} else {
			st.setObject("offset", 0);
		}

		// set parameters
		if (obj.getId() != Integer.MIN_VALUE) {
			st.setObject("id", obj.getId());
		} else {
			st.setObject("id", null);
		}
		if (obj.getIdAgenda() != Integer.MIN_VALUE) {
			st.setObject("id_agenda", obj.getIdAgenda());
		} else {
			st.setObject("id_agenda", null);
		}
		if (obj.getIdParent() != Integer.MIN_VALUE) {
			st.setObject("id_parent", obj.getIdParent());
		} else {
			st.setObject("id_parent", null);
		}
		if (obj.getName() != null) {
			st.setObject("name", obj.getName());
		} else {
			st.setObject("name", null);
		}
		if (obj.getDescription() != null) {
			st.setObject("description", obj.getDescription());
		} else {
			st.setObject("description", null);
		}
		if (obj.getRemark() != null) {
			st.setObject("remark", obj.getRemark());
		} else {
			st.setObject("remark", null);
		}
		if (obj.getIsGroupSet()) {
			st.setObject("is_group", obj.getIsGroup());
		} else {
			st.setObject("is_group", null);
		}
		if (obj.getIdPrevItem() != Integer.MIN_VALUE) {
			st.setObject("id_prev_item", obj.getIdPrevItem());
		} else {
			st.setObject("id_prev_item", null);
		}
		if (obj.getStartTimeFrom() != null) {
			st.setObject("start_timeFrom", obj.getStartTimeFrom());
		} else {
			st.setObject("start_timeFrom", null);
		}
		if (obj.getStartTimeTo() != null) {
			st.setObject("start_timeTo", obj.getStartTimeTo());
		} else {
			st.setObject("start_timeTo", null);
		}
		if (obj.getDuration() != Integer.MIN_VALUE) {
			st.setObject("duration", obj.getDuration());
		} else {
			st.setObject("duration", null);
		}
		if (obj.getIdEmployee() != Integer.MIN_VALUE) {
			st.setObject("id_employee", obj.getIdEmployee());
		} else {
			st.setObject("id_employee", null);
		}
		if (obj.getIdCompany() != Integer.MIN_VALUE) {
			st.setObject("id_company", obj.getIdCompany());
		} else {
			st.setObject("id_company", null);
		}
		if (obj.getSound() != null) {
			st.setObject("sound", obj.getSound());
		} else {
			st.setObject("sound", null);
		}
		if (obj.getLight() != null) {
			st.setObject("light", obj.getLight());
		} else {
			st.setObject("light", null);
		}
		if (obj.getLeftScreen() != null) {
			st.setObject("left_screen", obj.getLeftScreen());
		} else {
			st.setObject("left_screen", null);
		}
		if (obj.getRightScreen() != null) {
			st.setObject("right_screen", obj.getRightScreen());
		} else {
			st.setObject("right_screen", null);
		}
		if (obj.getCenterScreen() != null) {
			st.setObject("center_screen", obj.getCenterScreen());
		} else {
			st.setObject("center_screen", null);
		}
		if (obj.getCode() != null) {
			st.setObject("code", obj.getCode());
		} else {
			st.setObject("code", null);
		}
		if (obj.getIdType() != Integer.MIN_VALUE) {
			st.setObject("id_type", obj.getIdType());
		} else {
			st.setObject("id_type", null);
		}
		if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
			st.setObject("status", null);
		}
		if (obj.getNotification() != Integer.MIN_VALUE) {
			st.setObject("notification", obj.getNotification());
		} else {
			st.setObject("notification", null);
		}
		if (obj.getCreatedFrom() != null) {
			st.setObject("createdFrom", obj.getCreatedFrom());
		} else {
			st.setObject("createdFrom", null);
		}
		if (obj.getCreatedTo() != null) {
			st.setObject("createdTo", obj.getCreatedTo());
		} else {
			st.setObject("createdTo", null);
		}
		if (obj.getCreator() != Integer.MIN_VALUE) {
			st.setObject("creator", obj.getCreator());
		} else {
			st.setObject("creator", null);
		}
		if (obj.getUpdatedFrom() != null) {
			st.setObject("updatedFrom", obj.getUpdatedFrom());
		} else {
			st.setObject("updatedFrom", null);
		}
		if (obj.getUpdatedTo() != null) {
			st.setObject("updatedTo", obj.getUpdatedTo());
		} else {
			st.setObject("updatedTo", null);
		}
		if (obj.getUpdator() != Integer.MIN_VALUE) {
			st.setObject("updator", obj.getUpdator());
		} else {
			st.setObject("updator", null);
		}
		if (obj.getDeletedSet()) {
			st.setObject("deleted", obj.getDeleted());
		} else {
			st.setObject("deleted", null);
		}
		if (obj.getDeletionTimeFrom() != null) {
			st.setObject("deletion_timeFrom", obj.getDeletionTimeFrom());
		} else {
			st.setObject("deletion_timeFrom", null);
		}
		if (obj.getDeletionTimeTo() != null) {
			st.setObject("deletion_timeTo", obj.getDeletionTimeTo());
		} else {
			st.setObject("deletion_timeTo", null);
		}

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genInsertStatement(Connection dbConn, IModel model) throws SQLException {
		String sql = "SELECT sp_agenda_item_insert(:id_agenda,:id_parent,:name,:description,:remark,:is_group,:id_prev_item,:start_time,:duration,:id_employee,:id_company,:sound,:light,:left_screen,:right_screen,:center_screen,:code,:id_type,:status,:notification,:creator)";
		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		AgendaItemModel obj = (AgendaItemModel) model;

		// set parameters here
		if (obj.getIdAgenda() != Integer.MIN_VALUE) {
			st.setObject("id_agenda", obj.getIdAgenda());
		} else {
			st.setObject("id_agenda", null);
		}
		if (obj.getIdParent() != Integer.MIN_VALUE) {
			st.setObject("id_parent", obj.getIdParent());
		} else {
			st.setObject("id_parent", null);
		}
		if (obj.getName() != null) {
			st.setObject("name", obj.getName());
		} else {
			st.setObject("name", null);
		}
		if (obj.getDescription() != null) {
			st.setObject("description", obj.getDescription());
		} else {
			st.setObject("description", null);
		}
		if (obj.getRemark() != null) {
			st.setObject("remark", obj.getRemark());
		} else {
			st.setObject("remark", null);
		}
		if (obj.getIsGroupSet()) {
			st.setObject("is_group", obj.getIsGroup());
		} else {
			st.setObject("is_group", null);
		}
		if (obj.getIdPrevItem() != Integer.MIN_VALUE) {
			st.setObject("id_prev_item", obj.getIdPrevItem());
		} else {
			st.setObject("id_prev_item", null);
		}
		if (obj.getStartTime() != null) {
			st.setObject("start_time", obj.getStartTime());
		} else {
			st.setObject("start_time", null);
		}
		if (obj.getDuration() != Integer.MIN_VALUE) {
			st.setObject("duration", obj.getDuration());
		} else {
			st.setObject("duration", null);
		}
		if (obj.getIdEmployee() != Integer.MIN_VALUE) {
			st.setObject("id_employee", obj.getIdEmployee());
		} else {
			st.setObject("id_employee", null);
		}
		if (obj.getIdCompany() != Integer.MIN_VALUE) {
			st.setObject("id_company", obj.getIdCompany());
		} else {
			st.setObject("id_company", null);
		}
		if (obj.getSound() != null) {
			st.setObject("sound", obj.getSound());
		} else {
			st.setObject("sound", null);
		}
		if (obj.getLight() != null) {
			st.setObject("light", obj.getLight());
		} else {
			st.setObject("light", null);
		}
		if (obj.getLeftScreen() != null) {
			st.setObject("left_screen", obj.getLeftScreen());
		} else {
			st.setObject("left_screen", null);
		}
		if (obj.getRightScreen() != null) {
			st.setObject("right_screen", obj.getRightScreen());
		} else {
			st.setObject("right_screen", null);
		}
		if (obj.getCenterScreen() != null) {
			st.setObject("center_screen", obj.getCenterScreen());
		} else {
			st.setObject("center_screen", null);
		}
		if (obj.getCode() != null) {
			st.setObject("code", obj.getCode());
		} else {
			st.setObject("code", null);
		}
		if (obj.getIdType() != Integer.MIN_VALUE) {
			st.setObject("id_type", obj.getIdType());
		} else {
			st.setObject("id_type", null);
		}
		if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
			st.setObject("status", null);
		}
		if (obj.getNotification() != Integer.MIN_VALUE) {
			st.setObject("notification", obj.getNotification());
		} else {
			st.setObject("notification", null);
		}
		if (obj.getCreator() != Integer.MIN_VALUE) {
			st.setObject("creator", obj.getCreator());
		} else {
			st.setObject("creator", null);
		}

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genUpdateStatement(Connection dbConn, IModel model) throws SQLException {
		String sql = "SELECT sp_agenda_item_update(:id,:id_agenda,:id_parent,:name,:description,:remark,:is_group,:id_prev_item,:start_time,:duration,:id_employee,:id_company,:sound,:light,:left_screen,:right_screen,:center_screen,:code,:id_type,:status,:notification,:updator,:deleted,:deletion_time)";

		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		AgendaItemModel obj = (AgendaItemModel) model;

		// set parameters here
		if (obj.getId() != Integer.MIN_VALUE) {
			st.setObject("id", obj.getId());
		} else {
			st.setObject("id", null);
		}
		if (obj.getIdAgenda() != Integer.MIN_VALUE) {
			st.setObject("id_agenda", obj.getIdAgenda());
		} else {
			st.setObject("id_agenda", null);
		}
		if (obj.getIdParent() != Integer.MIN_VALUE) {
			st.setObject("id_parent", obj.getIdParent());
		} else {
			st.setObject("id_parent", null);
		}
		if (obj.getName() != null) {
			st.setObject("name", obj.getName());
		} else {
			st.setObject("name", null);
		}
		if (obj.getDescription() != null) {
			st.setObject("description", obj.getDescription());
		} else {
			st.setObject("description", null);
		}
		if (obj.getRemark() != null) {
			st.setObject("remark", obj.getRemark());
		} else {
			st.setObject("remark", null);
		}
		if (obj.getIsGroupSet()) {
			st.setObject("is_group", obj.getIsGroup());
		} else {
			st.setObject("is_group", null);
		}
		if (obj.getIdPrevItem() != Integer.MIN_VALUE) {
			st.setObject("id_prev_item", obj.getIdPrevItem());
		} else {
			st.setObject("id_prev_item", null);
		}
		if (obj.getStartTime() != null) {
			st.setObject("start_time", obj.getStartTime());
		} else {
			st.setObject("start_time", null);
		}
		if (obj.getDuration() != Integer.MIN_VALUE) {
			st.setObject("duration", obj.getDuration());
		} else {
			st.setObject("duration", null);
		}
		if (obj.getIdEmployee() != Integer.MIN_VALUE) {
			st.setObject("id_employee", obj.getIdEmployee());
		} else {
			st.setObject("id_employee", null);
		}
		if (obj.getIdCompany() != Integer.MIN_VALUE) {
			st.setObject("id_company", obj.getIdCompany());
		} else {
			st.setObject("id_company", null);
		}
		if (obj.getSound() != null) {
			st.setObject("sound", obj.getSound());
		} else {
			st.setObject("sound", null);
		}
		if (obj.getLight() != null) {
			st.setObject("light", obj.getLight());
		} else {
			st.setObject("light", null);
		}
		if (obj.getLeftScreen() != null) {
			st.setObject("left_screen", obj.getLeftScreen());
		} else {
			st.setObject("left_screen", null);
		}
		if (obj.getRightScreen() != null) {
			st.setObject("right_screen", obj.getRightScreen());
		} else {
			st.setObject("right_screen", null);
		}
		if (obj.getCenterScreen() != null) {
			st.setObject("center_screen", obj.getCenterScreen());
		} else {
			st.setObject("center_screen", null);
		}
		if (obj.getCode() != null) {
			st.setObject("code", obj.getCode());
		} else {
			st.setObject("code", null);
		}
		if (obj.getIdType() != Integer.MIN_VALUE) {
			st.setObject("id_type", obj.getIdType());
		} else {
			st.setObject("id_type", null);
		}
		if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
			st.setObject("status", null);
		}
		if (obj.getNotification() != Integer.MIN_VALUE) {
			st.setObject("notification", obj.getNotification());
		} else {
			st.setObject("notification", null);
		}
		if (obj.getUpdator() != Integer.MIN_VALUE) {
			st.setObject("updator", obj.getUpdator());
		} else {
			st.setObject("updator", null);
		}
		if (obj.getDeletedSet()) {
			st.setObject("deleted", obj.getDeleted());
		} else {
			st.setObject("deleted", null);
		}
		if (obj.getDeletionTime() != null) {
			st.setObject("deletion_time", obj.getDeletionTime());
		} else {
			st.setObject("deletion_time", null);
		}

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genDeleteStatement(Connection dbConn, IModel query) throws SQLException {
		String sql = "select sp_agenda_item_delete(:id);";
		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		AgendaItemModel obj = (AgendaItemModel) query;

		// set primary keys
		st.setObject("id", obj.getId());

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genCountStatement(Connection dbConn, IModelExt input) throws SQLException {
		String sql = "SELECT sp_agenda_item_count(:id,:id_agenda,:id_parent,:name,:description,:remark,:is_group,:id_prev_item,:start_timeFrom,:start_timeTo,:duration,:id_employee,:id_company,:sound,:light,:left_screen,:right_screen,:center_screen,:code,:id_type,:status,:notification,:createdFrom,:createdTo,:creator,:updatedFrom,:updatedTo,:updator,:deleted,:deletion_timeFrom,:deletion_timeTo)";

		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		AgendaItemModelExt obj = (AgendaItemModelExt) input;

		// set parameters here
		if (obj.getId() != Integer.MIN_VALUE) {
			st.setObject("id", obj.getId());
		} else {
			st.setObject("id", null);
		}
		if (obj.getIdAgenda() != Integer.MIN_VALUE) {
			st.setObject("id_agenda", obj.getIdAgenda());
		} else {
			st.setObject("id_agenda", null);
		}
		if (obj.getIdParent() != Integer.MIN_VALUE) {
			st.setObject("id_parent", obj.getIdParent());
		} else {
			st.setObject("id_parent", null);
		}
		if (obj.getName() != null) {
			st.setObject("name", obj.getName());
		} else {
			st.setObject("name", null);
		}
		if (obj.getDescription() != null) {
			st.setObject("description", obj.getDescription());
		} else {
			st.setObject("description", null);
		}
		if (obj.getRemark() != null) {
			st.setObject("remark", obj.getRemark());
		} else {
			st.setObject("remark", null);
		}
		if (obj.getIsGroupSet()) {
			st.setObject("is_group", obj.getIsGroup());
		} else {
			st.setObject("is_group", null);
		}
		if (obj.getIdPrevItem() != Integer.MIN_VALUE) {
			st.setObject("id_prev_item", obj.getIdPrevItem());
		} else {
			st.setObject("id_prev_item", null);
		}
		if (obj.getStartTimeFrom() != null) {
			st.setObject("start_timeFrom", obj.getStartTimeFrom());
		} else {
			st.setObject("start_timeFrom", null);
		}
		if (obj.getStartTimeTo() != null) {
			st.setObject("start_timeTo", obj.getStartTimeTo());
		} else {
			st.setObject("start_timeTo", null);
		}
		if (obj.getDuration() != Integer.MIN_VALUE) {
			st.setObject("duration", obj.getDuration());
		} else {
			st.setObject("duration", null);
		}
		if (obj.getIdEmployee() != Integer.MIN_VALUE) {
			st.setObject("id_employee", obj.getIdEmployee());
		} else {
			st.setObject("id_employee", null);
		}
		if (obj.getIdCompany() != Integer.MIN_VALUE) {
			st.setObject("id_company", obj.getIdCompany());
		} else {
			st.setObject("id_company", null);
		}
		if (obj.getSound() != null) {
			st.setObject("sound", obj.getSound());
		} else {
			st.setObject("sound", null);
		}
		if (obj.getLight() != null) {
			st.setObject("light", obj.getLight());
		} else {
			st.setObject("light", null);
		}
		if (obj.getLeftScreen() != null) {
			st.setObject("left_screen", obj.getLeftScreen());
		} else {
			st.setObject("left_screen", null);
		}
		if (obj.getRightScreen() != null) {
			st.setObject("right_screen", obj.getRightScreen());
		} else {
			st.setObject("right_screen", null);
		}
		if (obj.getCenterScreen() != null) {
			st.setObject("center_screen", obj.getCenterScreen());
		} else {
			st.setObject("center_screen", null);
		}
		if (obj.getCode() != null) {
			st.setObject("code", obj.getCode());
		} else {
			st.setObject("code", null);
		}
		if (obj.getIdType() != Integer.MIN_VALUE) {
			st.setObject("id_type", obj.getIdType());
		} else {
			st.setObject("id_type", null);
		}
		if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
			st.setObject("status", null);
		}
		if (obj.getNotification() != Integer.MIN_VALUE) {
			st.setObject("notification", obj.getNotification());
		} else {
			st.setObject("notification", null);
		}
		if (obj.getCreatedFrom() != null) {
			st.setObject("createdFrom", obj.getCreatedFrom());
		} else {
			st.setObject("createdFrom", null);
		}
		if (obj.getCreatedTo() != null) {
			st.setObject("createdTo", obj.getCreatedTo());
		} else {
			st.setObject("createdTo", null);
		}
		if (obj.getCreator() != Integer.MIN_VALUE) {
			st.setObject("creator", obj.getCreator());
		} else {
			st.setObject("creator", null);
		}
		if (obj.getUpdatedFrom() != null) {
			st.setObject("updatedFrom", obj.getUpdatedFrom());
		} else {
			st.setObject("updatedFrom", null);
		}
		if (obj.getUpdatedTo() != null) {
			st.setObject("updatedTo", obj.getUpdatedTo());
		} else {
			st.setObject("updatedTo", null);
		}
		if (obj.getUpdator() != Integer.MIN_VALUE) {
			st.setObject("updator", obj.getUpdator());
		} else {
			st.setObject("updator", null);
		}
		if (obj.getDeletedSet()) {
			st.setObject("deleted", obj.getDeleted());
		} else {
			st.setObject("deleted", null);
		}
		if (obj.getDeletionTimeFrom() != null) {
			st.setObject("deletion_timeFrom", obj.getDeletionTimeFrom());
		} else {
			st.setObject("deletion_timeFrom", null);
		}
		if (obj.getDeletionTimeTo() != null) {
			st.setObject("deletion_timeTo", obj.getDeletionTimeTo());
		} else {
			st.setObject("deletion_timeTo", null);
		}

		return st.getPreparedStatement();
	}

	/*
	 * private String fillHtmlTemplate(String template) { String html = template
	 * .replace(":id", Integer.toString(_id)) .replace(":id_agenda",
	 * Integer.toString(_id_agenda)) .replace(":id_parent",
	 * Integer.toString(_id_parent)) .replace(":name", HtmlUtil.genHtml(_name))
	 * .replace(":description", HtmlUtil.genHtml(_description))
	 * .replace(":remark", HtmlUtil.genHtml(_remark)) .replace(":is_group",
	 * HtmlUtil.genHtml(_isis_groupSet, _is_group)) .replace(":id_prev_item",
	 * Integer.toString(_id_prev_item)) .replace(":start_time",
	 * StringUtil.toDateTimeString(_start_time)) .replace(":duration",
	 * Integer.toString(_duration)) .replace(":id_employee",
	 * Integer.toString(_id_employee)) .replace(":id_company",
	 * Integer.toString(_id_company)) .replace(":sound",
	 * HtmlUtil.genHtml(_sound)) .replace(":light", HtmlUtil.genHtml(_light))
	 * .replace(":left_screen", HtmlUtil.genHtml(_left_screen))
	 * .replace(":right_screen", HtmlUtil.genHtml(_right_screen))
	 * .replace(":center_screen", HtmlUtil.genHtml(_center_screen))
	 * .replace(":code", HtmlUtil.genHtml(_code)) .replace(":id_type",
	 * Integer.toString(_id_type)) .replace(":status",
	 * Integer.toString(_status)) .replace(":notification",
	 * Integer.toString(_notification)) .replace(":created",
	 * StringUtil.toDateTimeString(_created)) .replace(":creator",
	 * Integer.toString(_creator)) .replace(":updated",
	 * StringUtil.toDateTimeString(_updated)) .replace(":updator",
	 * Integer.toString(_updator)) .replace(":deleted",
	 * HtmlUtil.genHtml(_isdeletedSet, _deleted)) .replace(":deletion_time",
	 * StringUtil.toDateTimeString(_deletion_time))
	 * 
	 * ; return html; }
	 */

	/*
	 * private String fillHtmlTemplate(String template) { String html = template
	 * .replace(":Id", Integer.toString(_Id)) .replace(":IdAgenda",
	 * Integer.toString(_IdAgenda)) .replace(":IdParent",
	 * Integer.toString(_IdParent)) .replace(":Name", HtmlUtil.genHtml(_Name))
	 * .replace(":Description", HtmlUtil.genHtml(_Description))
	 * .replace(":Remark", HtmlUtil.genHtml(_Remark)) .replace(":IsGroup",
	 * HtmlUtil.genHtml(_isIsGroupSet, _IsGroup)) .replace(":IdPrevItem",
	 * Integer.toString(_IdPrevItem)) .replace(":StartTime",
	 * StringUtil.toDateTimeString(_StartTime)) .replace(":Duration",
	 * Integer.toString(_Duration)) .replace(":IdEmployee",
	 * Integer.toString(_IdEmployee)) .replace(":IdCompany",
	 * Integer.toString(_IdCompany)) .replace(":Sound",
	 * HtmlUtil.genHtml(_Sound)) .replace(":Light", HtmlUtil.genHtml(_Light))
	 * .replace(":LeftScreen", HtmlUtil.genHtml(_LeftScreen))
	 * .replace(":RightScreen", HtmlUtil.genHtml(_RightScreen))
	 * .replace(":CenterScreen", HtmlUtil.genHtml(_CenterScreen))
	 * .replace(":Code", HtmlUtil.genHtml(_Code)) .replace(":IdType",
	 * Integer.toString(_IdType)) .replace(":Status", Integer.toString(_Status))
	 * .replace(":Notification", Integer.toString(_Notification))
	 * .replace(":Created", StringUtil.toDateTimeString(_Created))
	 * .replace(":Creator", Integer.toString(_Creator)) .replace(":Updated",
	 * StringUtil.toDateTimeString(_Updated)) .replace(":Updator",
	 * Integer.toString(_Updator)) .replace(":Deleted",
	 * HtmlUtil.genHtml(_isDeletedSet, _Deleted)) .replace(":DeletionTime",
	 * StringUtil.toDateTimeString(_DeletionTime))
	 * 
	 * ; return html; }
	 */

	// ADDED CODE

	private AgendaItemModel _parent = null;
	private List<AgendaItemModel> _childNodes = new ArrayList<AgendaItemModel>();
	private int _level = 0;
	private int _index = 0;

	@Override
	public int compareTo(ITreeNode o) {
		AgendaItemModel obj = (AgendaItemModel) o;
		int res = 0;

		if (_start_time != null && obj.getStartTime() != null) {
			res = _start_time.compareTo(obj.getStartTime());
		}

		// if (_id_parent == Integer.MIN_VALUE) {
		// res = _start_time.compareTo(obj.getStartTime());
		// } else {
		// res = _start_time.compareTo(obj.getStartTime());
		// }
		
		return res;
	}

	@Override
	public ITreeNode getParent() {
		return _parent;
	}

	@Override
	public void setParent(ITreeNode parent) {
		_parent = (AgendaItemModel) parent;
	}

	@Override
	public List<ITreeNode> getChildNodes() {
		List<ITreeNode> res = new ArrayList<ITreeNode>();
		for (AgendaItemModel obj : _childNodes) {
			res.add(obj);
		}
		return res;
	}

	@Override
	public void addChildNode(ITreeNode childNode) {
		_childNodes.add((AgendaItemModel) childNode);
	}

	@Override
	public int getLevel() {
		return _level;
	}

	@Override
	public void setLevel(int level) {
		_level = level;
	}

	public int getIndex() {
		return _index;
	}

	public void setIndex(int index) {
		_index = index;
	}

	private boolean _addedToTree = false;

	@Override
	public boolean getAddedToTree() {
		return _addedToTree;
	}

	@Override
	public void setAddedToTree(boolean added) {
		_addedToTree = added;
	}

}